package com.tm.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.article.service.ApArticleContentService;
import com.tm.media.model.entity.ApArticleContentEntity;
import com.tm.media.model.entity.ApUserBlankEntity;
import com.tm.media.model.entity.ApUserLetterEntity;
import com.tm.media.model.mapper.ApArticleContentMapper;
import com.tm.media.model.mapper.ApUserBlankMapper;
import com.tm.media.model.mapper.ApUserLetterMapper;
import com.tm.media.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Ly
 * @Data 2023/3/5 9:16
 * @ClassName ApArticleContentServiceImpl
 */
@Service
public class ApArticleContentServiceImpl extends ServiceImpl<ApArticleContentMapper, ApArticleContentEntity>implements ApArticleContentService {
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ApUserBlankMapper apUserBlankMapper;
    @Autowired
    private ApUserLetterMapper apUserLetterMapper;
    @Override
    public Result load_article_info(ApArticleContentEntity apArticleContentEntity) {
        QueryWrapper<ApArticleContentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id",apArticleContentEntity.getArticle_id());
        ApArticleContentEntity contentEntity = apArticleContentMapper.selectOne(queryWrapper);
        return Result.SUCCESS(contentEntity);
    }

    @Override
    public Result black(Integer id, Integer type) {
        //假设当前用户的ID是1
        ApUserBlankEntity apUserBlankEntity = new ApUserBlankEntity();
        apUserBlankEntity.setBlankId(id);
        apUserBlankEntity.setUserId(1);
        QueryWrapper<ApUserBlankEntity> queryWrapper = new QueryWrapper<>();
        //看一下有没有被拉黑。如果拉黑了，就释放出来
        queryWrapper.eq("user_id",1);
        queryWrapper.eq("blank_id",id);
        ApUserBlankEntity blankEntity = apUserBlankMapper.selectOne(queryWrapper);
        if (blankEntity==null){
            apUserBlankMapper.insert(apUserBlankEntity);
            return Result.SUCCESS(200,"拉黑成功");
        }else {
            apUserBlankMapper.deleteById(blankEntity.getId());
            return Result.SUCCESS(200,"取消成功");
        }
    }

    @Override
    public Result sixin(ApUserLetterEntity apUserLetterEntity) {
        //先看一下当前用户有没有拉黑
        QueryWrapper<ApUserBlankEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blank_id",apUserLetterEntity.getSenderId());
        queryWrapper.eq("user_id",apUserLetterEntity.getUserId());
        ApUserBlankEntity blankEntity = apUserBlankMapper.selectOne(queryWrapper);
        if (blankEntity==null){
            apUserLetterMapper.insert(apUserLetterEntity);
        }else {
            return Result.SUCCESS(500,"！您已被对方拉黑，发送失败");
        }
        return Result.SUCCESS(200,"已发送");
    }
}
